import allBus from '@/core/bus'
import { encrypt } from '@/utils/encrypt'

export default {
  type: 'page',
  style: {
    width: '400px',
    position: 'absolute',
    top: '30%',
    left: '50%',
    transform: 'translateX(-50%)'
  },
  body: {
    type: 'form',
    title: '商户后台',
    labelWidth: '100px',
    submitText: '登陆',
    rules: {
      username: [
        {
          required: true,
          message: '请输入用户名'
        }
      ],
      password: [
        {
          required: true,
          message: '请输入密码'
        }
      ]
    },
    api: {
      method: 'post',
      url: '/login',
      headers: {
        isToken: false
      },
      isDataSchema: true,
      data: {
        username: '${username}',
        password: '${password}',
        useLdapLogin: true
      },
      requestAdaptor: function (config) {
        const result = {
          ...config,
          data: {
            ...config.data,
            password: encrypt(config.data.password)
          }
        }
        return result
      },
      responseAdaptor: function (payload) {
        console.log('payload', payload)
        allBus.common.loginSuccess.emit(payload.token)
        return payload
      }
    },
    body: [
      {
        type: 'input-text',
        prop: 'username',
        label: '用户名',
        placeholder: '请输入用户名'
      },
      {
        type: 'input-password',
        prop: 'password',
        label: '密码',
        placeholder: '请输入密码'
      },
      {
        type: 'button',
        level: 'primary',
        actionType: 'submit',
        body: '登录'
      }
    ]
  }
}
